package cn.com.xinjinfu.pdoulcProduct.activity.FixedProduct;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseWebActivity;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.entity.ZhuanMingXiBean;
import cn.com.xinjinfu.pdoulcProduct.entity.ZhuanMingXiBean.Lists;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.utils.TextViewUtil;
import cn.com.xinjinfu.pdoulcProduct.xlistview.XListView;

/**
 * Created by android-adt on 2016/3/30.
 */
public class ZhuanrangMingxi extends BaseActivity implements View.OnClickListener {
    private User user;
    private XListView mListView;
    private RequestQueue requen;
    private MyAdapet mAdapter;
    private List<Lists> mingxiList;
    private String investId;
    private long transferDetailId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_zhuan_mingxi);
        TitleManager.showTitle(this, "转让明细", "");
        user = AccountManager.getInstance().getUser();
        Intent intent = getIntent();
        investId = intent.getStringExtra("investId");//得到对应的bidId值
        mListView = (XListView) findViewById(R.id.xListView);
        mListView.setPullLoadEnable(false);
        mListView.setPullRefreshEnable(false);
        mAdapter = new MyAdapet(this);
        mListView.setAdapter(mAdapter);
        requen = Volley.newRequestQueue(this);
        loginRequest();
    }

    /**
     * 网络请求
     */
    private void loginRequest() {
        if (user != null) {
            final Map<String, String> parameters = DataHandler.getParameters("223");
            parameters.put("id", user.getCurrUser().getSign());
            parameters.put("investId", investId);
            JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon, null);
            requen.add(request);
        }
    }

    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
//            L.d("转让合同 ==========" + jobj);
            ZhuanMingXiBean mingXiBean = JSON.parseObject(jobj.toString(), ZhuanMingXiBean.class);
            mingxiList = mingXiBean.getLists();
            if (mingxiList != null) {
                mAdapter.setMyList(mingxiList);
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_hetong:
                Intent intent = new Intent(this, BaseWebActivity.class);
                Bundle bundle = new Bundle();
                String url = DataHandler.WEBURL + "/front/wa/transferpact?transferDetailId=" + transferDetailId
                        + "&id=" + user.getCurrUser().getSign();
                bundle.putString("url", url);
                bundle.putString("title", "转让合同");
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }

    private class MyAdapet extends BaseAdapter {
        private LayoutInflater mInflater;
        private DateFormat sdf;
        private List<Lists> myList;


        public void setMyList(List list) {
            myList.clear();
            myList.addAll(list);
            notifyDataSetChanged();
        }

        public void addList(List list) {
            myList.addAll(list);
            notifyDataSetChanged();
        }

        public MyAdapet(Context context) {
            this.mInflater = LayoutInflater.from(context);
//            sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
            sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm");
            myList = new ArrayList<>();
        }

        @Override
        public int getCount() {
            return myList == null ? 0 : myList.size();
        }

        @Override
        public Object getItem(int position) {
            return myList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.time_item, null);
                holder = new ViewHolder();
                holder.v_xian = convertView.findViewById(R.id.v_xian);
                holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
                holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                holder.tv_hetong = (TextView) convertView.findViewById(R.id.tv_hetong);
                holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
                convertView.setTag(holder);//绑定ViewHolder对象
            } else {
                holder = (ViewHolder) convertView.getTag();//取出ViewHolder对象
            }
            Lists mingitem = myList.get(position);
            transferDetailId = mingitem.getId();
            holder.tv_hetong.setOnClickListener(ZhuanrangMingxi.this);
            holder.tv_money.setText(TextViewUtil.getSumLength(mingitem.getOffer_price()));
            holder.tv_name.setText(mingitem.getRealityName().substring(0, 1) + "**");
            holder.tv_time.setText(sdf.format(mingitem.getTime()));
            if (position == myList.size() - 1) {
                holder.v_xian.setVisibility(View.GONE);
            } else {
                holder.v_xian.setVisibility(View.VISIBLE);
            }
            return convertView;
        }
    }

    public final class ViewHolder {
        public View v_xian;
        public TextView tv_money;
        public TextView tv_name;
        public TextView tv_hetong;
        public TextView tv_time;
    }

}
